<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Some functions are derived from CakePHP,
 *   Copyright (c) 2006, Cake Software Foundation, Inc.
 *   http://cakephp.org/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package      FeatherPHP2
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

function & cleanArray(&$a)
{
	if (get_magic_quotes_gpc())
		__cleanArray($a);
	return $a;
}

function __cleanArray(&$a)
{
	if (is_array($a))
	{
		foreach(array_keys($a) as $k) {
			cleanArray($a[$k]);
		}
	}
	else {
		$a = stripslashes($a);
	}
}

function & array_intersect_keys(array $arr, array $keys)
{
	$rs = array();
	foreach($arr as $k => $v)
	{
		if (in_array($k, $keys))
			$rs[$k] = $v;
	}
	return $rs;
}

function s_()
{
	$args = func_get_args();
	$args[0] = _($args[0]);
	return call_user_func_array('sprintf', $args);
}

function debug()
{
	if (!DEBUG)
		return;
	foreach (func_get_args() as $v)
	{
		echo '<pre>';
		print_r($v);
		echo '</pre>';
	}
}

function f_basename($filename)
{
	preg_match(':^([^?#]+)[.]([^?#.]+):', basename($filename), $match);
	if (!empty($match))
		return array($match[1], $match[2]);
	return array($filename, null);
}

function implode_with_keys($glue, $linefeed, array &$arr)
{
	$data = '';
	foreach ($arr as $k => $v)
		$data .= $k.$glue.$v.$linefeed;
	return $data;
}

function env($key)
{
	if     (isset($_SERVER["ORIG_$key"])) return $_SERVER["ORIG_$key"];
	elseif (isset($_SERVER[$key]))        return $_SERVER[$key];
	elseif (isset($_ENV[$key]))           return $_ENV[$key];
	elseif (getenv($key) !== false)       return getenv($key);
	
	return null;
}

function cache($path, $data=null, $expires=Config::cache_lifetime, $target='cache')
{
	switch (strtolower($target))
	{
		case 'cache':  $filename = APP.'tmp'.DS.'cache'.DS.$path; break;
		case 'public': $filename = APP.'webroot'.DS.$path; break;
	}
	
	if (is_null($data))
	{
		// reads cache
		if (file_exists($filename))
		{
			$now = time();
			if (is_string($expires))
				$expires = strtotime($expires);
			if (filemtime($filename) + ($expires - $now) < $now)
			{
				// cache has expired
				@unlink($filename);
			}
			else
			{
				// cache is good!
				$data = file_get_contents($filename);
			}
		}
	}
	else
	{
		// creates cache
		file_put_contents($filename, $data, LOCK_EX);
	}
	
	return $data;
}

/*
function exec_cmd($cmd, $stdin, &$stdout)
{
	$descriptors = array(
		array('pipe', 'r'),             // stdin
		array('pipe', 'w'),             // stdout
		array('file', '/dev/null', 'a') // stderr
	);
	$proc = proc_open($cmd, &$descriptors, &$pipes);
	
	if (is_resource($proc))
	{
		// sends data to stdin
		fwrite($pipes[0], $stdin);
		fclose($pipes[0]);
		
		// receives output from stdout
		while(!feof($pipes[1]))
			$stdout .= fgets($pipes[1], 1024);
		fclose($pipes[1]);
		
		// ends process
		return proc_close($proc);
	}
	return false;
}
*/
?>